package com.dcbeida.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.StockDetail;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 库存
 */
public class SocketDetailAdapter extends BaseAdapter {

    private ArrayList<StockDetail.ListBean> mArrayList = new ArrayList<>();
    private Activity mContext;

    public SocketDetailAdapter(Activity context) {
        super();
        this.mContext = context;
    }


    @Override
    public int getCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return this.mArrayList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mArrayList == null) {
            return null;
        } else {
            return this.mArrayList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.stock_detail_item, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        StockDetail.ListBean bean = mArrayList.get(position);
        holder.tvBh.setText(bean.getBatchnumber());
        holder.tvYxrq.setText(mContext.getString(R.string.yxq) + bean.getExpiredate() + "");
        holder.tvBox.setText(bean.getQuantity() + "");
        holder.tv_name.setText(bean.getDrugname());

        if (bean.isFirst()) {
            holder.lin_header.setVisibility(View.VISIBLE);
            holder.tv_all_count.setText(bean.getCount() + "");
        } else {
            holder.lin_header.setVisibility(View.GONE);
        }

        if ("1".equals(bean.getIsalarm())) {
            holder.tvSyts.setText(bean.getExpiredays());
            holder.tvSyts.setVisibility(View.VISIBLE);
            holder.tvYxrq.setTextColor(mContext.getResources().getColor(R.color.c_C64848));
        } else {
            holder.tvSyts.setVisibility(View.GONE);
            holder.tvYxrq.setTextColor(mContext.getResources().getColor(R.color.c_333333));
        }

        return convertView;
    }


    public ArrayList<StockDetail.ListBean> getmArrayList() {
        return mArrayList;
    }

    public void setmArrayList(ArrayList<StockDetail.ListBean> mArrayList) {
        if (mArrayList.size() >= 0) {
            this.mArrayList.addAll(mArrayList);
        }
        notifyDataSetChanged();
    }

    public void clearList() {
        this.mArrayList.clear();
    }


    class ViewHolder {
        @Bind(R.id.tv_bh)
        TextView tvBh;
        @Bind(R.id.tv_yxrq)
        TextView tvYxrq;
        @Bind(R.id.tv_syts)
        TextView tvSyts;//剩余天数
        @Bind(R.id.tv_box)
        TextView tvBox;
        @Bind(R.id.tv_name)
        TextView tv_name;
        @Bind(R.id.tv_all_count)
        TextView tv_all_count;


        @Bind(R.id.lin_header)
        LinearLayout lin_header;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
